﻿@model DataAccess.DB.ChuyenKho

@{
    ViewBag.Title = "Chuyển kho";
}
<script src="~/Scripts/CustomValidate.js"></script>
<style>
    #NgayNhapKho {
        width: 120px;
    }

    #MaHoaDon {
        width: 140px;
    }
</style>

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
          
    <table>
        <tr>
            <td style="vertical-align: top">
                <fieldset>
                    <legend>Thông tin hóa đơn</legend>
                    <table>
                        <tr>
                            <td class="editor-label" style="width: 100px">
                                <span>Từ kho:</span><span style="color: red">*</span>
                            </td>
                            <td class="editor-field">
                                @Html.DropDownList("OldKhoID", String.Empty)<br />
                                @Html.ValidationMessageFor(model => model.OldKhoID)
                            </td>
                            <td class="editor-label" style="width: 150px">
                                <span>Nhân viên nhập kho:</span>
                                @Html.HiddenFor(model => model.UserID)
                            </td>
                            <td class="editor-field">
                                @ViewBag.CurrentUserDisplayName
                            </td>
                        </tr>
                        <tr>
                            <td class="editor-label">
                                <span>Đến kho:</span><span style="color: red">*</span>
                            </td>
                            <td class="editor-field">
                                @Html.DropDownList("NewKhoID", String.Empty)<br />
                                @Html.ValidationMessageFor(model => model.NewKhoID)
                            </td>
                            <td class="editor-label">
                                <span>Mã hóa đơn:</span><span style="color: red">*</span>
                            </td>
                            <td class="editor-field">
                                @Html.TextBoxFor(model => model.CodeChuyen, new { @Value = @ViewBag.MaHD, @maxlength = 15 })<br />
                                @Html.ValidationMessageFor(model => model.CodeChuyen)
                            </td>
                        </tr>
                        <tr>
                            <td class="editor-label">
                                
                            </td>
                            <td class="editor-field">
                                
                            </td>
                            <td class="editor-label">
                                <span>Ngày chuyển kho:</span><span style="color: red">*</span>
                            </td>
                            <td class="editor-field">
                                @Html.EditorFor(model => model.Time)<br />
                                @Html.ValidationMessageFor(model => model.Time)
                            </td>
                        </tr>
                    </table>
                </fieldset>
            </td>
        </tr>
    </table>
    <table id="users" class="ms-form-table-hd" style="width:600px">       
        <thead>
            <tr>
                <th style="width:250px">Mã sản phẩm</th>
                <th style="width:150px">Số lượng</th>
                <th style="width:150px">Đơn vị</th>
                <th style="width:50px"></th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>        
     
    <table>
        <tr>
            <td style="text-align: left">
                <input type="button" id="btnAddSP" class="btnInsertRows" />
            </td>
        </tr>
    </table>   

    <p>
        <input type="submit" value="Lưu hóa đơn" name="submit" />@Html.ActionLink("Thoát", "Index", null, new { @class = "linkCreate" })
    </p>

    <div id="dialog-modal" style="display: none" title="Danh sách sản phẩm">
    </div>
    <div id="dialog-confirm" style="display: none" title="Lưu hóa đơn">
        <span>Bạn có muốn lưu hóa đơn ?</span>
    </div> 
}

<input type="hidden" id="rowsp" />
<input type="hidden" id="stt" value="1" name="stt" />

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")

    <script type="text/javascript">
        $(document).ready(function () {
            $('#Time').datepicker();
            $("#UserID").val(@ViewBag.CurrentUserID);
            $("#UserID").val(@ViewBag.CurrentUserID);
        });

        //confirm submit
        var showDialog = true;
        $("input[name='submit']").click(function (e) {
            var input = $(this);
            var form = $(this).parents("form");
            if (showDialog) {
                // Prevent submit
                e.preventDefault();
                // Show dialog
                $("#dialog-confirm").dialog({
                    resizable: false,
                    height: 140,
                    modal: true,
                    buttons: {
                        'Lưu': function () {
                            $(this).dialog('close');
                            // Get the parent form
                            showDialog = false;
                            //form.submit();
                            //input.submit();
                            $(input).trigger("click");
                        },
                        'Không': function () {
                            $(this).dialog('close');
                            showDialog = true;
                        }
                    }
                });
            }
        });


        function showattach(val) {
            $("#rowsp").val(val);
            $("#dialog-modal").dialog({
                width: 1000,
                height: 600,
                modal: true,

                open: function () {
                    $(this).load("/SanPhamInKho/IndexSPCK");
                },
            });
        }
        $("#btnAddSP").click(function () {
            var stt = $("#stt").val();
            $("#stt").val(parseInt(stt) + 1);
            $("#users tbody").append("<tr>" +
              "<td><input type='text' id='ma_sp" + stt + "' name='ma_sp" + stt + "' style='width:150px'/> <input type='button' id='" + stt + "' name='ma_sp" + stt + "' value='+' onclick='showattach(this.id);' style='font-weight:bold;width:30px'/></td>" +
              "<td><input type='text' id='SoLuong_" + stt + "' style='width:100px;text-align:right' name='SoLuong_" + stt + "' value='0'/></td>" +
              "<td><input type='text' id='DonVi" + stt + "'style='width:100px' readonly='true' name='DonVi" + stt + "'/></td>" +
              "<td><input type='button' value='xóa' id='delete" + stt + "'style='width:40px' class='delrow'/></td>" +
            "</tr>");
            //add ma san pham

            $(".delrow").click(function () {
                $(this).parent().parent().remove();
            })
        });


        function SelectSP(sp) {
            var row = $("#rowsp").val();

            var selectedValue = sp;
            if (selectedValue != "Select") {
                $.ajax({
                    url: '@Url.Action("GetProductById", "SanPham")',
                        type: 'POST',
                        data: { "selectedValue": selectedValue },
                        dataType: 'json',
                        success: function (response) {
                            var productDetails = response.ProductDetail;
                            var productdetail = productDetails.split(';#');
                            $("#ma_sp" + row).val(productdetail[0]);
                            $("#DonVi" + row).val(productdetail[2]);
                        }
                    });
                $("#dialog-modal").dialog("close");
            }
        }

        $(':input').mouseover(function () {
            $(this).attr("title", $(this).val());
        });
    </script>
}
